/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-11-02
 * Time: 23:19
 */
import java.util.*;

public class Joseph {
    public int getResult(int n) {
        int max=0;//记录每轮报数的最大值
        LinkedList<Integer> queue=new LinkedList<>();
        LinkedList<Integer> emptyQueue=new LinkedList<>();
        for(int i=1;i<=n;i++){
            queue.add(i);
        }
        for(max=2;;max++){
            int count=0;
            if(queue.size()==1){
                return queue.remove();
            }
            while(queue.size()>0){
                count=count%max+1;
                if(count!=1){
                    //报的数不是1直接删除
                    queue.remove();
                }else{
                    emptyQueue.add(queue.remove());
                }
            }
            //队尾元素放到队头
            emptyQueue.addFirst(emptyQueue.pollLast());
            //交换两个队列
            LinkedList<Integer> temp=queue;
            queue=emptyQueue;
            emptyQueue=temp;
        }
    }
}
